/*
 * @Description: 
 * @Version: V1.0.0
 * @Author: liuxiaonan
 * @Date: 2023-11-20 08:54:46
 * @LastEditors: 刘逍男 178111805@qq.com
 * @LastEditTime: 2023-11-21 15:28:55
 * @FilePath: \electron-base\preload.js
 */
// const fs = require('fs')
// fs.writeFile('/Users/macos/Desktop/aaa.txt','abc',()=>{
//     console.log('done.')
// })

const { contextBridge, ipcRenderer, clipboard, nativeImage } = require("electron");
const handleSend = async () => {
    let fallback = await ipcRenderer.invoke('send-event', 'hahahaah')
    console.log(fallback)
}

const copy = () => {
    clipboard.writeText('HELLOW clipboard')
}
const show = () => {
    const text = clipboard.readText()
    console.log(text)
}
//nativeImage只能做本地的图片
const testNativeImage = () => {
    const image = nativeImage.createFromPath('start.png')
    console.log(image, image.getSize())
}
//可以用来从桌面捕获音频和视频的媒体源的信息
const captrue = async () => {
    let sources = await ipcRenderer.invoke('capture-event')
    // console.log(sources)
    for (const source of sources) {
        if (source.name === '润泽科技-DCIM综合管理平台 - Google Chrome') {
            // console.log(source)
            let str = source.thumbnail.crop({ x: 0, y: 30, width: 1200, height: 1170 })
            const imgSrc = str.toDataURL()
            return imgSrc;
        }
    }
}
contextBridge.exposeInMainWorld("myApi", {
    platform: process.platform,
    handleSend,
    copy,
    show,
    testNativeImage,
    captrue
});
// console.log(ipcRenderer)

